package com.dyh.quickdemozwei.util;

public class DeliveryBizContextTL {



    private static final ThreadLocal<DeliveryBizContext> tl = ThreadLocal.withInitial(
            DeliveryBizContext::new
    );

    public static DeliveryBizContext get(){
        return tl.get();
    }

    public static void clean(){
        tl.remove();
    }


    public static void main(String[] args) {
        DeliveryBizContextTL.get().setX("XXX");

        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+" : "+ DeliveryBizContextTL.get().getX());

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            DeliveryBizContextTL.get().setX("YYY");

            System.out.println(Thread.currentThread().getName()+" : "+ DeliveryBizContextTL.get().getX());
        }).start();

        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+" : "+ DeliveryBizContextTL.get().getX());

    }
}
